﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DatabaseCompare.Model;
using DatabaseCompare.Service.Compare;

namespace DatabaseCompare.ServiceImpl.Compare
{

    /// <summary>
    /// 列比较处理.
    /// </summary>
    public class ColumnCompare : 
        CompareAbleCompare<ColumnInfo>, 
        ICompareAbleCompare<ColumnInfo, ColumnCompareResult>
    {


        /// <summary>
        /// 两个列比较.
        /// </summary>
        /// <param name="leftColumn"></param>
        /// <param name="rightColumn"></param>
        /// <returns></returns>
        public ColumnCompareResult Compare(ColumnInfo left, ColumnInfo right)
        {
            // 比较结果.
            ColumnCompareResult result = new ColumnCompareResult();

            // 设置比较参数.
            result.Left = left;
            result.Right = right;

            // 首先简单作非空的判断.
            result.CompareResult = base.CommonCompare(left, right);


           if(result.CompareResult == CommonCompareResult.FullSame)
           {
                // 左右都不为空
                // 需要详细比较.

                // 开始比较.
                // 列的顺序
                if(left.ColumnId != right.ColumnId)
                {
                    result.CompareResult = CommonCompareResult.Difference;
                    result.ColumnIdDifference = true;
                }
                // 数据类型
                if(left.DataType != right.DataType)
                {
                    result.CompareResult = CommonCompareResult.Difference;
                    result.DataTypeDifference = true;
                }
                // 占用字节数
                if(left.MaxLength != right.MaxLength)
                {
                    result.CompareResult = CommonCompareResult.Difference;
                    result.MaxLengthDifference = true;
                }
                // 长度
                if(left.Precision != right.Precision)
                {
                    result.CompareResult = CommonCompareResult.Difference;
                    result.PrecisionDifference = true;
                }
                // 小数位数
                if(left.Scale != right.Scale)
                {
                    result.CompareResult = CommonCompareResult.Difference;
                    result.ScaleDifference = true;
                }
                // 是否允许非空
                if(left.IsNullAble != right.IsNullAble)
                {
                    result.CompareResult = CommonCompareResult.Difference;
                    result.IsNullAbleDifference = true;
                }
                // 是否是自增ID
                if(left.IsIDEntity != right.IsIDEntity)
                {
                    result.CompareResult = CommonCompareResult.Difference;
                    result.IsIDEntityDifference = true;
                }
                // 是否是主键
                if(left.IsPrimaryKey != right.IsPrimaryKey)
                {
                    result.CompareResult = CommonCompareResult.Difference;
                    result.IsPrimaryKeyDifference = true;
                }
                // 备注/说明信息
                if(left.Comment != right.Comment)
                {
                    result.CompareResult = CommonCompareResult.Difference;
                    result.CommentDifference = true;
                }
            }

            // 返回.
            return result;
        }


    }

}
